package com.hb.trade.server.center.sys.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.center.sys.entity.DistrictInfo;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.center.sys.service.DistrictInfoService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 地区信息表 前端控制器
 *
 * @author zhaojk
 * @since 2021-05-21
 */
@RestController
public class DistrictInfoController extends MPBaseController<DistrictInfo, DistrictInfoService> {

    public DistrictInfoController(DistrictInfoService service) {
        super(service);
    }

    /**
     * 分页查询
     *
     * @param districtInfo
     * @return
     */
    @PostMapping("/districtInfo/pageDistrict")
    public Message<PageResult<DistrictInfo>> pageDistrict(@RequestBody DistrictInfo districtInfo) {
        Page<DistrictInfo> districtInfoPage = districtInfo.pages();
        page(districtInfoPage);
        return MsgUtil.success(PageResult.get(districtInfoPage));
    }

    /**
     * 根据条件查询地区列表
     *
     * @param districtInfo
     * @return
     */
    @PostMapping("/districtInfo/listDistricts")
    public Message<List<DistrictInfo>> listDistricts(@RequestBody DistrictInfo districtInfo) {
        QueryWrapper<DistrictInfo> wrapper = districtInfo.buildWrapper();
        wrapper.select("id", "parent_id", "dis_name", "area_code", "postal_code");
        wrapper.orderByAsc("area_code");
        List<DistrictInfo> list = service.list(wrapper);
        return MsgUtil.success(list);
    }

    /**
     * 根据条件查询地区列表
     *
     * @param districtId
     * @return
     */
    @PostMapping("/districtInfo/districtsById")
    public DistrictInfo districtsById(@RequestBody(required=false) String districtId) {
        DistrictInfo districtInfo = service.getById(districtId);
        return districtInfo == null ? new DistrictInfo() : districtInfo;
    }

    /**
     * 新增或者修改
     *
     * @param districtInfo
     * @return
     */
    @PostMapping("/districtInfo/updateDistrictInfo")
    public Message<Boolean> updateDistrictInfo(@RequestBody DistrictInfo districtInfo) {
        boolean result = saveOrUpdate(districtInfo);
        return MsgUtil.success(result);
    }

    /**
     * 逻辑删除
     *
     * @param districtInfo
     * @return
     */
    @PostMapping("/districtInfo/deleteDistrict")
    public Message<Boolean> deleteDistrict(@RequestBody DistrictInfo districtInfo) {
        boolean result = super.markDeleteById(districtInfo.getId());
        return MsgUtil.success(result);
    }

    /**
     * 批量逻辑删除
     *
     * @param districtIds
     * @return
     */
    @PostMapping("/districtInfo/deleteDistricts")
    public Message<Boolean> deleteDistricts(@RequestBody List<Long> districtIds) {
        UpdateWrapper<DistrictInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("delete_flag", 1)
                .set("updateTime", DateUtil.date())
                .in("id", districtIds);
        boolean result = service.update(updateWrapper);
        return MsgUtil.success(result);
    }
}
